package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.view.View;
import android.widget.TextView;

import com.jess.arms.base.App;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HouseRentingShowDuanZuListContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseRentListParmarBean;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/04/2019 09:33
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HouseRentingShowDuanZuListPresenter extends BasePresenter<HouseRentingShowDuanZuListContract.Model, HouseRentingShowDuanZuListContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HouseRentingShowDuanZuListPresenter(HouseRentingShowDuanZuListContract.Model model, HouseRentingShowDuanZuListContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 设置筛选条件
     * @param houseRentListParmarBean
     * @param housePayWayBean
     * @return
     */
    public HouseRentListParmarBean setParmat(HouseRentListParmarBean houseRentListParmarBean,HousePayWayBean housePayWayBean)
{
    houseRentListParmarBean.setPage("1");
    //被清空的时候
    houseRentListParmarBean.setRentType(null);//类型
    houseRentListParmarBean.setRoomCount(null);//几室
    houseRentListParmarBean.setRentDetailList(null);//出租要求，房源特色 等筛选条件取数据字典id
    houseRentListParmarBean.setAcreage(null);//商铺面积
    houseRentListParmarBean.setRentRoom(null);//房屋类型
    houseRentListParmarBean.setDailyRent(null);//商铺 短租
    if (housePayWayBean==null)
    {
        return houseRentListParmarBean;
    }
    houseRentListParmarBean.setRentType(housePayWayBean.getCategory());
    if (houseRentListParmarBean.getRentDetailList()==null)
    {
        houseRentListParmarBean.setRentDetailList(new ArrayList<>());
    }
   if (housePayWayBean.getChildren()!=null)
   {

       for (HousePayWayBean housePayWayBean1:housePayWayBean.getChildren())
       {
           if ("roomCount".equals(housePayWayBean1.getCode()))
           {
               //几室几厅，单选
              for (HousePayWayBean housePayWayBean2:housePayWayBean1.getChildren())
              {
                  if (housePayWayBean2.isSelect())
                  {
                      houseRentListParmarBean.setRoomCount(housePayWayBean2.getDictId());//几室
                      break;
                  }
              }
           }else if ("rentDetailList".equals(housePayWayBean1.getCode()))
           {
               List<String> temRentDetailList=new ArrayList<>();
               //亮点 和要求
               for (HousePayWayBean housePayWayBean3:housePayWayBean1.getChildren())
               {
                   if (housePayWayBean3.isSelect())
                   {
                       temRentDetailList.add(housePayWayBean3.getDictId());
                   }
               }
               if (!temRentDetailList.isEmpty())
               {
                   houseRentListParmarBean.getRentDetailList().addAll(temRentDetailList);
               }

           }else if ("rentRoom".equals(housePayWayBean1.getCode()))
           {
               //合租的房屋类型 独立房间
               for (HousePayWayBean housePayWayBean4:housePayWayBean1.getChildren())
               {
                   if (housePayWayBean4.isSelect())
                   {
                       houseRentListParmarBean.setRentRoom(housePayWayBean4.getDictId());//独立房间
                       break;
                   }
               }
           }else if ("acreage".equals(housePayWayBean1.getCode()))
           {
               //商业的 面积
               for (HousePayWayBean housePayWayBean5:housePayWayBean1.getChildren())
               {
                   if (housePayWayBean5.isSelect())
                   {
                       houseRentListParmarBean.setAcreage(housePayWayBean5.getDictId());//面积
                       break;
                   }
               }
           }else if ("dailyRent".equals(housePayWayBean1.getCode()))
           {
               //商业的 是否短租
               for (HousePayWayBean housePayWayBean6:housePayWayBean1.getChildren())
               {
                   if (housePayWayBean6.isSelect())
                   {
                       houseRentListParmarBean.setDailyRent(housePayWayBean6.getDictId());//是否短租
                       break;
                   }
               }
           }else if ("transferOrLease".equals(housePayWayBean1.getCode()))
           {
               //商业的 转租还是商铺
               for (HousePayWayBean housePayWayBean7:housePayWayBean1.getChildren())
               {
                   if (housePayWayBean7.isSelect())
                   {
                       houseRentListParmarBean.setTransferOrLease(housePayWayBean7.getDictId());
                       break;
                   }
               }
           }

       }
   }

    return houseRentListParmarBean;
}

    /**
     * 获取租房筛选条件
     * @param houseCategory
     * @param rentType
     */
   public  void  getQueryConditionTree(String houseCategory, String rentType) {
     mModel.getQueryConditionTree(houseCategory,rentType).compose(RxUtils.applySchedulers(mRootView))
             .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                 @Override
                 public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                    if (AppUtil.isSuccess(listResultBean))
                    {
                        mRootView.getQueryConditionTreeSuccess(listResultBean.getData());
                    }else {
                        mRootView.showMessage(listResultBean.getMessage());
                    }
                 }
             });
    }

    /**
     * 获取出租房展示列表
     * @param houseRentListParmarBean
     */
    public void getHouseRentList(boolean isrefersh,HouseRentListParmarBean houseRentListParmarBean){
        if (isrefersh)
        {
                mRootView.getHouseRentingShowListAdapter().setNewData(null);
        }

      mModel.getHouseRentList(AppUtil.entityToMap(houseRentListParmarBean)).compose(RxUtils.applySchedulers(mRootView))
              .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowBean>>>(mErrorHandler) {
                  @Override
                  public void onNext(ResultBean<List<HouseRentListShowBean>> listResultBean) {
                      if (AppUtil.isSuccess(listResultBean))
                      {
                          mRootView.getHouseRentListSuccess(listResultBean);
                      }else {
                          mRootView.showMessage(listResultBean.getMessage());
                      }

                  }
              });
    }

    /**
     * 获取付款方式,获取字典的都从这个接口获取
     * @param value
     * @param category
     */
    public void getHousePayWayBean(String value, String category,List<HousePayWayBean> housePayWayBeans)
    {
        if (housePayWayBeans!=null&&!housePayWayBeans.isEmpty())
        {
            mRootView.getHousePayWayBeanSuccess(value,housePayWayBeans);
            return;
        }
   if (value.equals(Api.HOUSE_AREA))
   {
       mModel.getAreaTree(Api.APP_DOMAIN_ZIDIAN+"/app/baseArea/getAreaTree",value,category).compose(RxUtils.applySchedulers(mRootView))
           .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
               @Override
               public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                   if (AppUtil.isSuccess(listResultBean))
                   {
                       mRootView.getHousePayWayBeanSuccess(value,listResultBean.getData());
                   }else {
                       mRootView.showMessage(listResultBean.getMessage());
                   }

               }
           });

       return;
   }
        mModel.getHousePayWayBean(Api.APP_DOMAIN_ZIDIAN+"/app/dictList",value,category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.getHousePayWayBeanSuccess(value,listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }

    /**
     * 得到界面筛选时候使用的数据结构
     * @param houseInfoConfigurations
     * @param houseInfofy
     * @return
     */
    public  List<HouseInfoTitleBean> getData(List<HousePayWayBean> houseInfoConfigurations,List<HousePayWayBean> houseInfofy){

        List<HouseInfoTitleBean> houseInfoTitleBeans=new ArrayList<>();
        HouseInfoTitleBean houseInfoTitleBean1=new HouseInfoTitleBean("房源");
        HouseInfoTitleBean houseInfoTitleBean2=new HouseInfoTitleBean("出租要求");
        HouseInfoTitleBean houseInfoTitleBean3=new HouseInfoTitleBean("房源特色");
        List<HousePayWayBean> houseInfoConfigurations2=new ArrayList<>();
        List<HousePayWayBean> houseInfoConfigurations3=new ArrayList<>();
        //    短租-房屋亮点    HOUSE_RENT_DETAIL_DZ  2
//    短租-出租要求    HOUSE_RENT_DETAIL_DZ  3
        for (HousePayWayBean houseInfoConfiguration:houseInfoConfigurations)
        {
            if ("2".equals(houseInfoConfiguration.getCategory())){
                houseInfoConfigurations2.add(houseInfoConfiguration);
            }else  if ("3".equals(houseInfoConfiguration.getCategory())){
                houseInfoConfigurations3.add(houseInfoConfiguration);
            }
        }
        HousePayWayBean housePayWayBean=null;
        housePayWayBean=new HousePayWayBean();
        housePayWayBean.setSelect(true);
        housePayWayBean.setName("不限");
        houseInfofy.add(0,housePayWayBean);
        houseInfoConfigurations2.add(0,housePayWayBean);
        houseInfoConfigurations3.add(0,housePayWayBean);
        houseInfoTitleBean1.setPayChileds(houseInfofy);//房源
        houseInfoTitleBean2.setPayChileds(houseInfoConfigurations2);//亮点
        houseInfoTitleBean3.setPayChileds(houseInfoConfigurations3);//要求
        houseInfoTitleBeans.add(houseInfoTitleBean1);
        houseInfoTitleBeans.add(houseInfoTitleBean2);
        houseInfoTitleBeans.add(houseInfoTitleBean3);
        return houseInfoTitleBeans;
    }

    /**\
     * 当用户点了按钮后把用户已经选的条件还原，下次打开的时候是还原状态
     * @param houseInfoTitleBeans
     */
    public void reductionStutas(List<HouseInfoTitleBean> houseInfoTitleBeans)
    {
        for (HouseInfoTitleBean houseInfoTitleBean:houseInfoTitleBeans)
        {
            for (int i=0;i<houseInfoTitleBean.getPayChileds().size();i++)
            {
                if (i!=0)
                {
                    houseInfoTitleBean.getPayChileds().get(i).setSelect(false);
                }else {
                    houseInfoTitleBean.getPayChileds().get(i).setSelect(true);
                }
            }
        }
    }

    /**
     * 得到参数
     * @param type
     * @return
     */
    public List<String> getparmar(String type,List<HouseInfoTitleBean> houseInfoTitleBeans)
    {
        List<String> selectId=new ArrayList<>();
        if(type.equals(Api.LANDLORD_TYPE))
        {
            for (HouseInfoTitleBean houseInfoTitleBean:houseInfoTitleBeans)
            {
                if (houseInfoTitleBean.getTitleName().equals("房源"))
                {
                    if (houseInfoTitleBean.getPayChileds().get(0).isSelect())
                    {
                        return selectId;
                    }
                    for (HousePayWayBean housePayWayBean:houseInfoTitleBean.getPayChileds())
                    {
                        if (housePayWayBean.isSelect())
                        {
                            selectId.add(housePayWayBean.getDictId());
                        }
                    }
                }
            }

        }else {
            for (HouseInfoTitleBean houseInfoTitleBean:houseInfoTitleBeans)
            {
                if (houseInfoTitleBean.getTitleName().equals("出租要求")||houseInfoTitleBean.getTitleName().equals("房源特色"))
                {
                    for (HousePayWayBean housePayWayBean:houseInfoTitleBean.getPayChileds())
                    {
                        if (housePayWayBean.isSelect())
                        {
                            selectId.add(housePayWayBean.getDictId());
                        }
                    }
                }
            }
        }
        return  selectId;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
